import hashlib
import re
from django.shortcuts import reverse, redirect, HttpResponse
from django.utils.deprecation import MiddlewareMixin


def get_md5(value):
    m = hashlib.md5('123'.encode('utf-8'))
    if isinstance(value, str):
        value = value.encode('utf-8')
    m.update(value)
    return m.hexdigest()


class PermissionMiddleware(MiddlewareMixin):
    def process_request(self, request):
        # 白名单  注册登录
        white_list = [reverse('select:login'), reverse('select:register')]
        if request.path in white_list:
            return
        # 是否登录
        is_login = request.session.get('is_login', None)
        if not is_login:
            return redirect('select:login')
        # admin
        admin_re = r'/admin/.*'
        if re.match(admin_re, request.path):
            if request.session.get('user', None) != 'zhangmengnan':
               return HttpResponse('<h1>您没有该权限</h1>')
            else:
                return